package cn.wolfcode.crm.web.controller;

import cn.wolfcode.crm.domain.Department;
import cn.wolfcode.crm.service.IPermissionService;
import cn.wolfcode.crm.util.RequiredPermission;
import cn.wolfcode.crm.qo.EmployeeQueryObject;
import cn.wolfcode.crm.qo.JsonResult;
import cn.wolfcode.crm.qo.PageResult;
import cn.wolfcode.crm.service.IDepartmentService;
import com.github.pagehelper.PageInfo;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/department")
public class DepartmentController {
    @Autowired
    private IDepartmentService departmentService;
    @Autowired
    private IPermissionService permissionService;

    @RequestMapping("/list")
    @RequiresPermissions(value = {"department:list", "部门列表"}, logical = Logical.OR)
    public String list(Model model, @ModelAttribute("qo") EmployeeQueryObject qo) {
        PageInfo<Department> pageResult = departmentService.query(qo);
        model.addAttribute("result", pageResult);
        return "/department/list";
    }

    @RequestMapping("/delete")
    @RequiresPermissions(value = {"department:delete", "部门删除"}, logical = Logical.OR)
    @ResponseBody
    public JsonResult delete(Long id) {
        try {
            departmentService.delete(id);
        } catch (Exception e) {
            e.printStackTrace();
            return new JsonResult(false, "操作失败");
        }
        return new JsonResult(true, "操作成功");
    }

    /*@RequestMapping("/input")
    @RequiredPermission(name="部门编辑", expression = "department:input")
    public String input(Model model,Long id){
        if (id != null){

            Department department = departmentService.selectOne(id);
            model.addAttribute("department",department);
        }
        return "/department/input";
    }*/
    @RequestMapping("saveOrUpdate")
    @RequiredPermission(name = "部门新增或者修改", expression = "department:saveOrUpdate")
    @ResponseBody
    public JsonResult saveOrUpdate(Department department) {
        try {
            if (department.getId() == null) {
                departmentService.save(department);

            } else {
                departmentService.update(department);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return new JsonResult(false, "操作失败");
        }
        return new JsonResult(true, "操作成功");
    }


}
